package com.wangshuo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wangshuo.common.MenuHelper;
import com.wangshuo.common.RouterHelper;
import com.wangshuo.domain.SysMenu;
import com.wangshuo.domain.SysRole;
import com.wangshuo.domain.SysRoleMenu;
import com.wangshuo.mapper.SysRoleMenuMapper;
import com.wangshuo.model.vo.AssginMenuVo;
import com.wangshuo.model.vo.RouterVo;
import com.wangshuo.service.SysMenuService;
import com.wangshuo.mapper.SysMenuMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
* @author 13326
* @description 针对表【sys_menu(菜单表)】的数据库操作Service实现
* @createDate 2025-02-06 17:45:31
*/
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu>
    implements SysMenuService{
    @Resource
    private SysMenuMapper sysMenuMapper;
    @Resource
    private SysRoleMenuMapper sysRoleMenuMapper;
    @Override
    public List<SysMenu> nodeList(){
        List<SysMenu> list=sysMenuMapper.selectList(null);
       if(CollectionUtils.isEmpty(list)){
           return null;
       }
       List<SysMenu> list1= MenuHelper.buildTree(list);
       return list1;

    }
    @Override
    public  void doAssign(AssginMenuVo assignMenuVo){
        LambdaQueryWrapper<SysRoleMenu> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(SysRoleMenu::getRoleId,assignMenuVo.getRoleId());
        sysRoleMenuMapper.delete(queryWrapper);
        for(String menuId :assignMenuVo.getMenuIdList()){
            if(menuId!=null){
                SysRoleMenu sysRoleMenu=new SysRoleMenu();
                sysRoleMenu.setMenuId(Long.valueOf(menuId));
                sysRoleMenu.setRoleId(Long.valueOf(assignMenuVo.getRoleId()));
                sysRoleMenuMapper.insert(sysRoleMenu);
            }
        }
    }
    @Override
    public     List<RouterVo> findUserMenuList(Long id){
        List<SysMenu> sysMenus=null;
        LambdaQueryWrapper<SysMenu> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysMenu::getStatus,1);
        if(id.longValue() == 1){

            sysMenus=sysMenuMapper.selectList(lambdaQueryWrapper);
        }else{
            sysMenus=sysMenuMapper.findByUserId(id);
        }
        List<SysMenu> sysMenus1=MenuHelper.buildTree(sysMenus);
        List<RouterVo> routerVoList= RouterHelper.buildRouters(sysMenus1);
        return routerVoList;

    }

}




